package com.pdg.mcplugin.invisiblehand.commandprocessors;

import org.bukkit.command.CommandSender;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.invisiblehand.InvisibleHand;
import com.pdg.mcplugin.invisiblehand.dataproviders.Commodity;
import com.pdg.mcplugin.invisiblehand.dataproviders.CommodityKey;

public class PriceCommandProcessor extends CommandProcessorBase<InvisibleHand> {

	private static final int ARGUMENT_COUNT = 0;
	private static final String MESSAGE_NOT_ENOUGH_ARGUMENTS = "Not enough arguments";
	private static final String MESSAGE_NO_SUCH_COMMODITY = "No such commodity";
	private static final String MESSAGE_SALES_PRICE = "The sales price of %d units of '%s' in '%s' is %f.";//TODO format currency
	private static final String MESSAGE_PURCHASE_PRICE = "The purchase price of %d units of '%s' in '%s' is %f.";//TODO format currency
	private static final String MESSAGE_INVALID_COUNT = "Invalid count!";

	public PriceCommandProcessor(InvisibleHand thePlugin) {
		super(thePlugin, new String[]{
				"/ih price - help with the price command",
				"/ih price (count) (commodity-name) - get the commodity price of (commodity-name) for (count) units in the current world",
				"/ih price (count) (commodity-name) (world) - get the commodity price of (commodity-name) for (count) units in (world)",
		});
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		long theCount;
		try{
			theCount = Long.parseLong(theSubCommand);
		}catch(NumberFormatException e){
			theCommandSender.sendMessage(MESSAGE_INVALID_COUNT);
			return false;
		}
		String theWorld = Utilities.getWorldIfPlayer(theCommandSender);
		if(!Utilities.checkArgumentCount(theCommandSender, theWorld, theArgumentList, ARGUMENT_COUNT, MESSAGE_NOT_ENOUGH_ARGUMENTS)){
			return false;
		}
		String theCommodityName = theArgumentList.pop() ;
		if(theWorld==null){
			theWorld = Utilities.getWorldName(theArgumentList);
		}
		CommodityKey theKey = new CommodityKey(theCommodityName,theWorld);
		Commodity theCommodity = getPlugin().getDataProvider().getCommodityTableProvider().retrieve(theKey);
		if(theCommodity==null){
			theCommandSender.sendMessage(MESSAGE_NO_SUCH_COMMODITY);
			return false;
		}
		double theSalesPrice = theCommodity.getSalesPrice(theCount);
		double thePurchasePrice = theCommodity.getPurchasePrice(theCount);
		theCommandSender.sendMessage(String.format(MESSAGE_SALES_PRICE,theCount,theCommodityName,theWorld,theSalesPrice));//TODO format currency
		theCommandSender.sendMessage(String.format(MESSAGE_PURCHASE_PRICE,theCount,theCommodityName,theWorld,thePurchasePrice));//TODO format currency
		return false;
	}

}
